<?php
require_once('libs/VakataServer/class.db.php');
require_once('libs/VakataServer/class.tree.php');

if(isset($_GET['operation'])) {
    $allowData = array(
        'ten',
        'type',
        'mamathang',
        'donvitinh',
        /*'soluong',*/
        'ghichu'
    );
    $fs = new Tree(db::get('mysqli://root@127.0.0.1/quanlybanhang'), array('structure_table' => 'mathang_struct', 'data_table' => 'mathang_data', 'data' => $allowData));
    try {
        $rslt = null;
        switch($_GET['operation']) {
            case 'analyze':
                var_dump($fs->analyze(true));
                die();
                break;
            case 'get_node':
                $node = isset($_GET['id']) && $_GET['id'] !== '#' ? (int)$_GET['id'] : 0;
                $temp = $fs->get_children($node);
                $rslt = array();
                foreach($temp as $v) {
                    $rslt[] = array('id' => $v['id'], 'text' => $v['ten'], 'type' => $v['type'], 'children' => ($v['rgt'] - $v['lft'] > 1));
                }
                break;
            case "get_data":
                $node = isset($_GET['id']) && $_GET['id'] !== '#' ? $_GET['id'] : 0;
                $temp = $fs->get_node((int)$node);

                /*$rslt = array(
                    'name'  => $temp['ten'],
                    'value' => $temp['query']
                );*/
                $rslt = $temp;
                break;
            case 'create_node':
                /*$node = isset($_GET['id']) && $_GET['id'] !== '#' ? (int)$_GET['id'] : 0;
                $temp = $fs->mk($node, isset($_GET['position']) ? (int)$_GET['position'] : 0, array(
                    'ten' => isset($_GET['text']) ? $_GET['text'] : 'Mặt hàng mới',
                    'type'  => $_GET['type'],
                    'mamathang' => $_GET['MaMatHang'],
                    'donvitinh' => $_GET['DonViTinh'],
                    'ghichu'    => $_GET['GhiChu'],
                ));
                $rslt = array('id' => $temp);
                break;*/

                $saveData = array(
                    'ten'       => isset($_GET['text']) ? $_GET['text'] : 'Mặt hàng mới',
                    'type'      => $_GET['type'],
                    'mamathang' => $_GET['MaMatHang'],
                    'donvitinh' => $_GET['DonViTinh'],
                    'soluong'   => $_GET['SoLuong'],
                    'ghichu'    => $_GET['GhiChu'],
                );
                $node = isset($_GET['id']) && $_GET['id'] !== '#' ? (int)$_GET['id'] : 0;
                $temp = $fs->mk($node, isset($_GET['position']) ? (int)$_GET['position'] : 0, $saveData);
                $rslt = array('id' => $temp);
                break;
            case 'save_info':
                $node = isset($_GET['id']) && $_GET['id'] !== '#' ? $_GET['id'] : 0;
                $fs->rn(
                        $node,
                        array(
                            'ten'       => $_POST['TenMatHang'],
                            'mamathang' => $_POST['MaMatHang'],
                            'donvitinh' => $_POST['DonViTinh'],
                            'soluong'   => $_POST['SoLuong'],
                            'ghichu'    => $_POST['GhiChu'],
                        )
                );
                break;
            case 'rename_node':
                $node = isset($_GET['id']) && $_GET['id'] !== '#' ? (int)$_GET['id'] : 0;
                $rslt = $fs->rn($node, array('ten' => isset($_GET['text']) ? $_GET['text'] : 'Renamed node'));
                break;
            case 'delete_node':
                $node = isset($_GET['id']) && $_GET['id'] !== '#' ? (int)$_GET['id'] : 0;
                $rslt = $fs->rm($node);
                break;
            case 'move_node':
                $node = isset($_GET['id']) && $_GET['id'] !== '#' ? (int)$_GET['id'] : 0;
                $parn = isset($_GET['parent']) && $_GET['parent'] !== '#' ? (int)$_GET['parent'] : 0;
                $rslt = $fs->mv($node, $parn, isset($_GET['position']) ? (int)$_GET['position'] : 0);
                break;
            case 'copy_node':
                $node = isset($_GET['id']) && $_GET['id'] !== '#' ? (int)$_GET['id'] : 0;
                $parn = isset($_GET['parent']) && $_GET['parent'] !== '#' ? (int)$_GET['parent'] : 0;
                $rslt = $fs->cp($node, $parn, isset($_GET['position']) ? (int)$_GET['position'] : 0);
                break;
            default:
                throw new Exception('Unsupported operation: ' . $_GET['operation']);
                break;
        }
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode($rslt);
    }
    catch (Exception $e) {
        header($_SERVER["SERVER_PROTOCOL"] . ' 500 Server Error');
        header('Status:  500 Server Error');
        echo $e->getMessage();
    }
    die();
}
?>